// formPackage/pages/appointment/appointmentList/appointmentList.js
import {
  queryBookingManagerList,
  dealBookingManager
} from "../../../../service/manager_service"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    dataList: [

    ],
    page: 0,
    pageSize: 100,
    status: "-1",
    exStatus: 0,
    switchArr: [
      {
        title: "全部",
        key: "-1",
      },
      {
        title: "预约中",
        key: 1,
      },
      {
        title: "预约成功",
        key: 2
      },
      {
        title: "预约失败",
        key: 3
      },
    ],
    /**
     * bookingStatus
     * 1:预约中。。。
     * 2:预约成功
     * 3:预约失败...
     */

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  switchAction(e) {
    let index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;
    this.setData({
      exStatus: index,
      status: item.key
    })
    this.queryBookingManagerList();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
    this.queryBookingManagerList();
  },
  //获取预约列表
  queryBookingManagerList() {
    wx.showLoading();
    queryBookingManagerList(this.data.page, this.data.pageSize, this.data.status).then(res => {
      wx.hideLoading();
      console.log("JJLog:预约列表是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          dataList: res.data || []
        })
      }
    }).catch(err => {
      console.error("JJLog:查询失败", err);
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      }, 3500)
    })
  },

  //预约操作
  bookingAction(e) {
    // let index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;
    if (item.status == 1) {
      //只有预约中可以操作:
      wx.showModal({
        title: '提示',
        content: '是否预约通过',
        cancelText: "不通过",
        confirmColor: "#FF0000",
        success: res => {
          if (res.confirm) {
            this.dealBookingManager(item.id, true);
          } else if (res.cancel) {
            this.dealBookingManager(item.id, false);
          }
        }
      })

    }
  },
  dealBookingManager(id, isPass) {
    wx.showLoading();
    dealBookingManager(id, isPass).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '操作成功',
          icon: "success"
        })
        this.queryBookingManagerList();
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作成功",
        icon: "none"
      })
      console.error("JJLog:dealBookingManager", err);
    })
  },












  pushToPublish() {
    wx.navigateTo({
      url: '/formPackage/pages/appointment/myAppointment/myAppointment',
    })
  },





})